<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Models\Forwarder;
use App\Models\Order;
use App\Models\OrderParcel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    public function index(Request $request)
    {
        $query = new Order();
        if ($request->input('status')) {
            $query = $query->where('status', $request->input('status'));
        }
        if ($request->input('forwarder') == 1) {
            $forwarder = Forwarder::where('user_id', $request->user()->id)->where('status', 1)->first();
            $query = $query->where('forwarder_id', $forwarder->id);
        } else {
            $query = $query->where('user_id', $request->user()->id);
        }
        if ($request->input('extends')){
            $query->with(explode(',', $request->input('extends')));
        }
        $list = $query->with('orderParcels')->orderBy('id', 'desc')->paginate();
        return $this->response($list);
    }

    public function view($id, Request $request)
    {
        $obj = Order::with('orderParcels')->where('id',$id)->first();
        return $this->response($obj);
    }

    public function store(Request $request, Order $obj)
    {
        DB::beginTransaction();
        try {
            $user_id = $request->user()->id;
            $all = $request->all();
            $obj->fill($all);
            $obj->user_id = $user_id;
            $obj->status = 0;
            $obj->price = 0;
            $obj->save();
            foreach ($request->input('parcel') as $item) {
                $orderParcel = new OrderParcel();
                $item['user_id'] = $user_id;
                $item['order_id'] = $obj->id;
                if($item['pack_type'] === 0){
                    $item['weight_all'] =  $item['weight'] * $item['num'];
                }
                $orderParcel->fill($item);
                $orderParcel->save();
            }
            DB::commit();
            return $this->response($obj->id);
        } catch (\Exception $exception) {
            DB::rollBack();
            abort(5070);
        }
    }

    public function take(Request $request){
        $order_id = $request->input('order_id');
        $order = Order::where('id', $order_id)->first();
        if($order->status != 0){
            abort(5071);
        }
        $order->status = 1;
        $order->forwarder_id = $request->forwarder_id;
        $order->save();
        return $this->response();
    }

    public function cancel(Request $request){
        $order = Order::where('id', $request->input('order_id'))->where('user_id', $request->user()->id)->first();
        $order->status = 3;
        $order->save();
        return $this->response();
    }

    public function confirm(Request $request){
        $order = Order::where('id', $request->input('order_id'))->where('user_id', $request->user()->id)->first();
        $order->status = 2;
        $order->price = $request->input('price');
        $order->save();
        return $this->response();
    }

    public function update(Request $request)
    {
        $obj = Order::where('user_id', $request->user()->id)->where('status', 0)->first();
        $obj->update($request->all());
        return $this->response();
    }

    public function destroy($id)
    {
        $obj = Order::find($id);
        $obj->delete();
        return $this->response();
    }
}
